<?php

/**
 * 评论管理
 * @author Administrator
 */
class comment extends admin{

   public function __construct(){
     parent::__construct();
   }

   public function index(){
      $school_id=(int)$_SESSION['client']['jx_id'];
      $where =' where school_id='.$school_id;
      if (isset($_GET['user_name']) && $_GET['user_name'] != '')  $where .= ' and user_name="' . $_GET['user_name'] . '"';
      if (isset($_GET['star']) && (int) $_GET['star'] > 0) $where .= ' and star=' . (int) $_GET['star'];
      if (isset($_GET['obj_table']) && $_GET['obj_table'] != '')  $where .= ' and obj_table="' . $_GET['obj_table'] . '"';
      // 总条数
      $total = db()->getOne('select count(*) from x_comment '.$where);
      $pagesize = C('PAGE_SIZE');
      if (isset($_GET['p']) && (int) $_GET['p'] > 0) $p = (int) $_GET['p'];
      else $p = 1;
      $limit = ' limit ' . ($p - 1) * $pagesize . ',' . $pagesize;
      $list = db()->getAll('select * from x_comment '.$where.' order by id DESC '. $limit);
      if(!$list) $list=array();
      //教练评价查x_user，驾校评价查x_jiaxiaoinfo，不分类全要查
      else {
         if($_GET['obj_table']!='user'){
            foreach ($list as $k=>$v) {
               $n = db()->getOne('select name from x_jiaxiaoinfo where id='.$v['obj_id']);
               if($n) $list[$k]['real_name']=$n;
            }
         }
         if($_GET['obj_table']!='jiaxiaoinfo'){
            foreach ($list as $k=>$v) {
               $n = db()->getOne('select real_name from x_user where id='.$v['obj_id']);
               if($n) $list[$k]['real_name']=$n;
            }
         }
      }
      $page = array('total' => $total,'pagesize'=>$pagesize,'curpage'=>$p);
      $this->ajaxReturn(array('status'=>1,'data'=>$list,'page'=>$page));
   }

   public function update(){
      $id = (int) $_POST['id'];
      $data['status'] = (int) $_POST['status'];
      $data['content'] = remove_xss($_POST['content']);
      $affert_num = db()->update('x_comment', $data, 'id=' . $id);
      if ($affert_num!==FALSE) {
         if ($affert_num>0) insertlogs('update','修改评论(ID:'.$id.')',$data,$id);
         $this->ajaxReturn(array( 'status' => '1', 'info' => '操作成功' ));
      }
      else $this->ajaxReturn(array( 'status' => '0', 'info' => '操作失败' ));
   }
}
